\documentclass[t,12pt,aspectratio=169]{beamer} % 16:9 宽屏比例，适合现代投影
\usepackage{ctex} % 中文支持
\usepackage{amsmath, amssymb} % 数学公式与符号
\usepackage{graphicx}
\usepackage{pythonhighlight}
\usepackage{url}
\usepackage{hyperref}
\usepackage{verbatim}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 插入另一个文档中的python代码
\usepackage{listings}
\usepackage{color}

% 设置列表的样式
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\lstset{style=mystyle}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 主题设置（推荐简洁风格）
\usetheme{Madrid}
\usecolortheme{default} % 可选：seahorse, beaver, dolphin 等

\title{应用回归分析第4章：违背基本假设的情况 }
\author{HXQ ET AL}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\begin{frame}
  \titlepage
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{第4章目录 }

\begin{enumerate}

\item[4.1.] 异方差性产生的背景和原因
\item[4.2.] 一元加权最小二乘估计
\item[4.3.] 多元加权最小二乘估计
\item[4.4.] 自相关性问题及其处理
\item[4.5.] BOX-COX变换
\item[4.6.] 异常值与强影响点

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.1.1. 线性回归模型的变量、数据、模型与任务}

\begin{itemize}

\item 设有自变量 $x_1,x_2,\cdots,x_p$ 和因变量 $y$. 
%\[ (x_{i1},x_{i2},\cdots, x_{ip},y_i), \,\, i=1,2,\cdots,n. \]

\item  设有 $n$ 组观测数据，写成表格的形式：
\begin{center}
\begin{tabular}{|c|cccc|c|} \hline 
变量  & $x_{1}$ & $x_{2}$ & $\cdots$ & $x_{p}$ & $y$ \\ \hline 
数据$1$ & $x_{11}$ & $x_{12}$ & $\cdots$ & $x_{1p}$ & $y_1$ \\ \hline 
数据$2$ & $x_{21}$ & $x_{22}$ & $\cdots$ & $x_{2p}$ & $y_2$ \\ \hline 
$\vdots$ & $\vdots$  & $\vdots$ & $\vdots$  & $\vdots$ & $\vdots$ \\ \hline
数据$n$ & $x_{n1}$ & $x_{n2}$ & $\cdots$ & $x_{np}$ & $y_n$ \\ \hline 
\end{tabular}
\end{center}

\item 多元线性回归模型如下，其中 $\beta_0,\beta_1,\cdots,\beta_p$ 为待估计的参数：
\[ {\color{red} y_i = \beta_0+\beta_1x_{i1}+\beta_2x_{i2}+\cdots+\beta_{p}x_{ip}+\varepsilon_i,\,\,\, i=1,2,\cdots,n} \]

\item 任务之一是用自变量 $x_1,x_2,\cdots,x_p$ 来解释和预测因变量 $y$.


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.1.2. 多元线性回归模型的基本假设}

\begin{enumerate}

\item 自变量 $x_1,x_2,\cdots,x_p$ 是确定的观测值，相互不影响。%具体地说，是指上述表格的 $p$ 个列向量线性无关，最好是互相垂直。

\item 因变量与自变量之间的关系是近似线性的，即 
\[ y_i = \beta_0+\beta_1x_{i1}+\beta_2x_{i2}+\cdots+\beta_{p}x_{ip}+\varepsilon_i,\,\,\, i=1,2,\cdots,n\]

\item (Gauss-Makov条件) 误差项 $\varepsilon_1,\varepsilon_2,\cdots,\varepsilon_n$ 是均值为零、方差相同、且两两不相关的随机变量，即
{\color{red}
\begin{eqnarray*}
\left\{\begin{array}{ll}
\mathbb{E}(\varepsilon_i) = 0, \,\,\, \textrm{var}(\varepsilon_i) = \sigma^2, & i=1,2,\cdots n\\
\textrm{cov}(\varepsilon_i,\varepsilon_j) = 0, &  i\neq j, \,\, i,j=1,2,\cdots n
\end{array}\right.
\end{eqnarray*}}

\vspace{-0.2cm}

\item 我们还经常假设误差项服从独立同分布的正态分布，即 
\begin{eqnarray*}
\left\{\begin{array}{ll}
\varepsilon_i \sim N(0,\sigma^2), & i=1,2,\cdots n\\
\textrm{cov}(\varepsilon_i,\varepsilon_j) = 0, &  i\neq j, \,\, i,j=1,2,\cdots n
\end{array}\right.
\end{eqnarray*}

\end{enumerate}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.1.3. 异方差的概念}

\begin{itemize}

\item 异方差是指实际中误差项的方差并不相等，即
\begin{eqnarray*}
{\color{red}\textrm{var}(\varepsilon_i) \neq \textrm{var}(\varepsilon_j) , \,\, i\neq j }
\end{eqnarray*}

%\item 
%
%\item 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.1.4. 异方差的例子1：城镇居民收入与消费水平}

\begin{itemize}

\item 设第 $i$ 户居民的收入为 $x_i$, 消费额为 $y_i$. 设一共考察了 $n$ 户居民。%，于是 $1\le i\le n$.

\item 考虑消费额对收入的回归模型
\begin{eqnarray*}
\left\{\begin{array}{l}
y_1 = \beta_0+\beta_1x_{1}+\varepsilon_1, \\
y_2 = \beta_0+\beta_1x_{2}+\varepsilon_2, \\
\cdots \cdots \\
y_n = \beta_0+\beta_1x_{n}+\varepsilon_n.
\end{array}\right.
\end{eqnarray*}

\item 一般情况，低收入家庭的购买差异较小，高收入家庭的消费差异较大。
\item {\color{red} 第 $i$ 个误差项 $\varepsilon_i$ 的方差 $\textrm{var}(\varepsilon_i)$ 与第 $i$ 个收入 $x_i$ 的大小有关。}

\item 结论：这个线性回归模型可能会有异方差现象。
%这里画一个说明异方差的图。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.1.5. 异方差的例子2：企业的生产函数}

\begin{itemize}

\item 设 $K$ 和 $L$ 分别是资本和劳动力，考虑企业的生产函数：
\[ Y=AK^\alpha L^\beta e^\varepsilon. \]

\item 取对数后是一个二元线性回归模型
\[ \ln Y = \ln A + \alpha \ln K + \beta \ln L + \varepsilon.\]

\item 误差项 $\varepsilon$ 偏离均值0的程度与每个企业的状况有关，导致该模型的不同数据的方差不同。设有两个企业的线性模型
\begin{eqnarray*}
\ln Y_i = \ln A + \alpha \ln K_i + \beta \ln L_i + \varepsilon_i \\
\ln Y_j = \ln A + \alpha \ln K_j + \beta \ln L_j + \varepsilon_j
\end{eqnarray*}

\item {\color{red} 异方差性是指这两个误差项的方差并不相等：
\begin{eqnarray*}
\textrm{var}(\varepsilon_i) \neq \textrm{var}(\varepsilon_j).
\end{eqnarray*}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.1.6. 异方差带来的问题}

\vspace{0.2cm}

如果实际数据存在异方差，那么普通最小二乘法有下述问题：

\begin{itemize}

\item {\color{red} 最小二乘估计会低估 $\hat{\beta}$  的真实的方差 $\textrm{var}(\hat{\beta})$.}

\item 因为对参数 $\beta$ 的检验的 $t$ 统计量是 $t=\frac{\hat{\beta}}{\sqrt{\textrm{var}(\hat{\beta})}}$, 从而会高估 $t$ 检验值，本来不显著的回归系数变成显著的。

\item 不正确的模型会影响回归方程的应用效果。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.1. 残差图分析法}

\begin{enumerate}
\item 以自变量 $x$ 为横坐标，残差 $e$ 为纵坐标，画出残差图。
\item 如果是等方差的，那么残差的离散程度应该与 $x$ 无关。
\item {\color{red} 如果观察到残差的`振幅'与 $x$ 有关，则认为存在异方差。}
\end{enumerate}

\begin{center}
\includegraphics[height=0.4\textheight,width=0.9\textwidth]{ex5-1-yfc.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.2. 异方差的检验方法2：等级相关系数法}

\begin{enumerate}

\item 设自变量 $x$ 的数据向量为 $(x_1,x_2,\cdots,x_n)$. 
\item 第 $i$ 个数据 $x_i$ 在其中的排名与第 $i$ 个残差绝对值 $|e_i|$ 在残差绝对值向量 $(|e_1|,\cdots,|e_n|)$ 中的排名的差记为 $d_i$. 则等级相关系数定义为 
{\color{red} \[r_s=1-\frac{6}{n(n^2-1)}\sum\limits_{i=1}^{n} (d_i)^2 \]}

\item 定义等级相关系数检验统计量
\( t = \frac{\sqrt{n-2}r_s}{\sqrt{1-r_s^2}} \).

\item 如果 $|t|> t_{\alpha/2}(n-2)$, 则认为残差与自变量有关，存在异方差问题。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.3. 计算等级相关系数的一个例子}

\begin{itemize}

\item 问题：设两位裁判给7位运动员的打分如下，问是否一致？

\begin{center}
\begin{tabular}{|c|ccccccc|} \hline 
运动员 & A & B & C & D & E & F & G  \\ \hline 
裁判甲 & $3$ & $5$ & $7$ & $4$ & $2$ & $1$ & $6$ \\ \hline
裁判乙 & $2$ & $4$ & $5$ & $6$ & $3$ & $1$ & $7$ \\ \hline
 $d$ = 名次的差 & $1$ & $1$ & $2$ & $-2$ & $-1$ & $0$ & $-1$ \\ \hline 
\end{tabular}
\end{center}

\item 解答：按照 Spearman 等级相关系数的定义，结果是比较一致。
\begin{eqnarray*}
{\color{red} r_s} &{\color{red} =}& {\color{red} 1-\frac{6}{n(n^2-1)}\sum\limits_{i=1}^{n} (d_i)^2} \\
 &=& 1-\frac{6\times(1+1+4+4+1+0+1)}{(7)\times (7^2-1)} 
 = 0.7857. 
\end{eqnarray*}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.4. 例子4.3. 居民收入与储蓄额（问题、变量、数据）}

\begin{itemize}

\item 问题：设有某地区的居民收入 $x$ 与储蓄额 $y$ 的数据。
\begin{enumerate}
\item 使用普通最小二乘法建立 $y$  对 $x$ 的回归方程，画出残差图。
\item 分析这个回归模型是否存在异方差现象。
\end{enumerate}

\item 数据文件：data-example-4-3.txt. 
 
\begin{center}
\begin{tabular}{|c|c|c|} \hline 
序号 & 储蓄额 $y$ （万元） & 居民收入 $x$ （万元）  \\ \hline 
1 & 264 & 8777 \\ \hline
2 & 105 & 9210 \\ \hline
3 & 90 & 9954 \\ \hline
$\vdots$ & $\vdots$ & $\vdots$ \\ \hline
30 & 2100 & 36200 \\ \hline
31 & 2300 & 38200 \\ \hline
\end{tabular}
\end{center}
 
\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.5. 例子4.3.（残差图检验异方差）}

\begin{itemize}
\item  以自变量 $x$ 为横坐标，残差 $e$ 为纵坐标，画出残差图。
\item  观察到残差的`振幅'与 $x$ 有关，认为存在异方差。
\end{itemize}

\begin{center}
\includegraphics[height=0.5\textheight,width=0.7\textwidth]{example-4-3-cct.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.6. 例子4.3.（等级相关系数检验异方差）}

\begin{itemize}

\item 等级 = 从小到大的序号，这个例子，取值1-31.
\item 分别计算自变量 $x$ 的等级和残差绝对值 $|e|$ 的等级。
\item 计算等级相关系数，得到 $r=0.686$.
\item 计算 $t$ 统计量的值，得到 $t=5.08$. 
\item 设显著性水平 $\alpha=0.05$. 
\item 自由度为 $n-2=29$ 的 $t$ 分布的分位数为 $t_{\alpha/2}(29)=2.0452$.
\item 拒绝残差绝对值与自变量不相关的零假设，认为存在异方差现象。

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.2.7. 例子4.3.（程序）} %若使用verbatim环境
%\begin{frame}{4.2.7. 例子4.3.（程序）}

{\small
\begin{lstlisting}[language=R]
mydata<-read.table('data-example-4-3.txt',head=T,sep='\t')
lm01<-lm(y~x,data=mydata)
e<-resid(lm01)
plot(mydata$x,e,ylim=c(-500,500),col='red')
abline(h=0,lty=5)
x<-mydata$x
mydata$xrank<-rank(x)
mydata$e<-e
mydata$abse<-abs(e)
mydata$abs.e.rank<-rank(abs(e))
mydata$di<-mydata$xrank-mydata$abs.e.rank
rs<-1-sum(mydata$di^2)*6/31/(31^2-1)
ts<-sqrt(31-2)*rs/sqrt(1-rs^2)
\end{lstlisting}
}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.8. 原理辨析：加权是如何处理异方差问题的(1)}

\begin{itemize}

\item 设有一元线性回归模型
\[ y = \beta_0 + \beta_1x+\varepsilon \]

\item 普通最小二乘法使得{\color{red} 离差平方和}达到最小：
\[ Q(\beta_0,\beta_1) = \sum\limits_{i=1}^{n} (y_i-\beta_0-\beta_1x_i)^2 \]

\item 加权最小二乘法使得{\color{red} 加权的离差平方和}达到最小，误差项 $\varepsilon_i$ 的方差 $\sigma_i^2$ 较大的项，相应的权重 $w_i$ 较小。
 \[ Q(\beta_0,\beta_1) = \sum\limits_{i=1}^{n} {\color{red}w_i} (y_i-\beta_0-\beta_1x_i)^2 \]

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.9. 原理辨析：加权是如何处理异方差问题的(2)}

\begin{itemize}

\item 设有 $n$ 组观测数据
\begin{center}
\begin{tabular}{|c|c|c|c|c|}\hline
$y$ & $x$ & 模型 & 误差的方差 & 权重 \\ \hline
$y_1$ & $x_1$ & $y_1=\beta_0+\beta_1x_1+\varepsilon_1$ & $\sigma_1^2=\textrm{var}(\varepsilon_1)$ & $w_1$ \\ \hline
$y_2$ & $x_2$ & $y_2=\beta_0+\beta_1x_2+\varepsilon_2$ & $\sigma_2^2=\textrm{var}(\varepsilon_2)$ & $w_2$ \\ \hline
$\cdots$ & $\cdots$ & $\cdots$  & $\cdots$ & $\cdots$  \\ \hline
$y_n$ & $x_n$ & $y_n=\beta_0+\beta_1x_n+\varepsilon_n$ & $\sigma_n^2=\textrm{var}(\varepsilon_n)$ & $w_n$ \\ \hline
\end{tabular}
\end{center}

\item 选取权重使得误差项的方差相等，
{\color{red}\[ \boxed{w_1\sigma_1^2 = w_2\sigma_2^2 = \cdots = w_n\sigma_n^2} \]}
这样就去除了异方差性，符合线性回归模型的基本假设了。

\item 因此选取权重 $w_i$, 与误差项的方差 $\sigma_i^2$ 成反比例。

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.10. 原理辨析：加权是如何处理异方差问题的(3)}

\begin{itemize}

\item 设因变量 $y$, 设计矩阵 $X$ 和权重 $W$ 分别如下。
\begin{eqnarray*}
y=\begin{bmatrix} y_1 \\ y_2 \\ \cdots \\ y_n    \end{bmatrix},
X=\begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ \cdots \\ 1 & x_n    \end{bmatrix},
W=\begin{bmatrix} w_1 & 0 & \cdots & 0 \\ 0 & w_2 & \cdots & 0 \\ \cdots \\ 0 & 0 & \cdots & w_n \end{bmatrix}.
\end{eqnarray*}

\item 加权最小二乘法的参数估计公式为
\begin{eqnarray*}
{\color{red}\hat{\boldsymbol\beta}= \begin{bmatrix} \hat{\beta}_0 \\ \hat{\beta}_1\end{bmatrix} = (X^TWX)^{-1}X^TWy}
\end{eqnarray*}

\item 比较普通最小二乘法的估计公式 {\color{red}$\hat{\boldsymbol\beta}=(X^TX)^{-1}X^Ty$}, 加权最小二乘的估计公式相当于先将第 $i$ 组数据 $(1,x_i,y_i)$ 改成了 $(\sqrt{w_i},\sqrt{w_i}x_i,\sqrt{w_i}y_i)$.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.11. 例子4.3.（寻找最优的加权）}

\begin{itemize}

\item  问题：处理异方差问题的方法有哪些？
\item  回答：加权最小二乘法、BOX-COX变换法、方差稳定变换法、等。

\vspace{0.5cm}

\item  问题：如何选取权函数？
\item  解答：在社会经济问题中，经常选取权函数与自变量的幂函数成反比，即 $w_i = 1/x_i^m, (1\le i\le n)$, 其中的 $m$ 待定。可以选取使得{\color{red}模型的对数似然统计量}最大的那个 $m$. 

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.12. 例子4.3.（加权最小二乘法的残差图）}

\begin{itemize}
\item 选取 $m=1.5$, 画出残差图。
\item 比较普通最小二乘法和加权最小二乘法的残差图，看不出明显的差异。加权真的起作用了吗？
\end{itemize}

\begin{center}
\includegraphics[height=0.5\textheight,width=0.7\textwidth]{example-4-3-cct-jq.png}
\end{center}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.13. 例子4.3.（加权对残差的影响）}

\begin{itemize}

\item 问题：比较普通最小二乘法的残差，与加权最小二乘法的残差。

\item 解答：将数据分成三组：
\begin{itemize}
\item 小方差组：
\item 中等方差组：
\item 大方差组：
\end{itemize}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.2.14. 加权最小二乘法的局限}

\begin{itemize}

\item 问题：解释加权最小二乘法的不足。

\item 解答：
\begin{itemize}
\item  可以构造数据，其回归模型有明显的异方差性，但是普通最小二乘法和加权最小二乘法的回归方程是一样的。（这样的例子如何构造？）
\item  加权最小二乘法以大方差项的拟合效果为代价改善小方差项的拟合效果（为什么？），这可能不符合实际情况。
\end{itemize}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.3.1. 多元线性回归模型中的加权最小二乘法}

\begin{itemize}

\item 设有二元线性回归模型
\[ y = \beta_0 + \beta_1x_1+ \beta_2x_2 + \varepsilon. \]

\item 普通最小二乘法使得{\color{red} 离差平方和}达到最小：
\[ Q(\beta_0,\beta_1,\beta_2) = \sum\limits_{i=1}^{n} (y_i-\beta_0-\beta_1x_{i1}-\beta_1x_{i2})^2. \]

\item 加权最小二乘法使得{\color{red} 加权的离差平方和}达到最小，
%误差项 $\varepsilon_i$ 的方差 $\sigma_i^2$ 较大的项，相应的权重 $w_i$ 较小。
 \[ Q(\beta_0,\beta_1) = \sum\limits_{i=1}^{n} {\color{red}w_i} (y_i-\beta_0-\beta_1x_{i1}-\beta_1x_{i2})^2. \]


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.3.2. 加权最小二乘法的矩阵形式}

\begin{itemize}

\item 设因变量 $y$, 设计矩阵 $X$, 权重矩阵 $W$, 和参数估计量 $\hat{\boldsymbol\beta}$ 分别如下。
\begin{eqnarray*}
y=\begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_n    \end{bmatrix},
X=\begin{bmatrix} 1 & x_{11} & x_{12} \\ 1 & x_{21} & x_{22} \\ \vdots&\vdots&\vdots \\ 1 & x_{n1} & x_{n2}    \end{bmatrix},
W=\begin{bmatrix} w_1 & 0 & \cdots & 0 \\ 0 & w_2 & \cdots & 0 \\ \vdots &\vdots&&\vdots \\ 0 & 0 & \cdots & w_n \end{bmatrix}, 
\hat{\boldsymbol\beta} = \begin{bmatrix} \hat{\beta}_0 \\ \hat{\beta}_1 \\ \hat{\beta}_2   \end{bmatrix}.
\end{eqnarray*}

\item 普通与加权的最小二乘法的参数估计公式分别为
\begin{eqnarray*}
\hat{\boldsymbol\beta} &=& (X^TX)^{-1}X^Ty, \\
\hat{\boldsymbol\beta} &=&  = (X^TWX)^{-1}X^TWy.
\end{eqnarray*}

\item 比较可知，加权最小二乘的参数估计相当于先将第 $i$ 组数据 $(1,x_{i1},x_{i2},y_i)$ 变成了 $(\sqrt{w_i},\sqrt{w_i}x_{i1},\sqrt{w_i}x_{i2},\sqrt{w_i}y_i)$.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.3.3. 例子4.4. 经济开发区的招商投资（问题）}

\begin{itemize}
\item 问题：研究北京市各经济开发区的经济发展与招商投资的关系。
\item 变量：
\begin{itemize}
\item 自变量：$x_1$ 为各开发区的累计招商数目，$x_2$ 为招商企业的注册资本。
\item 因变量：$y$ 为各开发区的销售收入。
\end{itemize}
\item 数据：data-example-3-2.txt.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.3.4. 例子4.4. 经济开发区的招商投资（程序）} %若使用verbatim环境
%\begin{frame}{4.3.4. 例子4.4. 经济开发区的招商投资（程序）}

\begin{lstlisting}[language=R]
> mydata<-read.table('data-example-3-2.txt',sep='\t',head=T)

> # 普通最小二乘法
> lm03<-lm(y~x1+x2,data=mydata)
> summary(lm03)
> e<-resid(lm03)
> abse<-abs(e)
> cor.test(mydata$x1,abse,method='spearman')
> cor.test(mydata$x2,abse,method='spearman')

> # 加权最小二乘法
> lm04<-lm(y~x1+x2,data=mydata,weight=x2^(-0.25))
> summary(lm04)
\end{lstlisting}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.3.5. 例子4.4. （两个模型的残差图）}

\begin{center}
\includegraphics[height=0.7\textheight,width=0.7\textwidth]{example-4-4-cct.png}
\end{center}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.3.6. 例子4.4. （结果分析）}

\begin{itemize}

\item 普通最小二乘法：残差与自变量 $x2$ 的等级相关系数的检验显著。
\item 选取权函数 $w_i = x_{\, i\,2}^{\, - 0.25}, \,\, (1\le i \le 15)$. （为什么？）
\item 比较两个模型的 $R^{\,2}$ 与 $F$ 值，加权模型更好一些。
\item 比较两个回归方程。（我的运算结果跟课文里的不一样）

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.1. 自相关的概念} 

\begin{itemize}

\item 线性回归模型的基本假设为：

    \begin{enumerate}
    
    \item 自变量 $x_1,x_2,\cdots,x_p$ 是确定的观测值，相互不影响。
    
    \item 因变量与自变量之间的关系是近似线性的，即 
    
    \vspace{-0.4cm}
    
    \[ y_i = \beta_0+\beta_1x_{i1}+\beta_2x_{i2}+\cdots+\beta_{p}x_{ip}+\varepsilon_i,\,\,\, i=1,2,\cdots,n\]

    \item 误差项 $\varepsilon_1,\varepsilon_2,\cdots,\varepsilon_n$ 的均值为零、方差相同、且两两不相关，即
        
    \vspace{-0.6cm}
    
    \begin{eqnarray*}
    \left\{\begin{array}{ll}
    \mathbb{E}(\varepsilon_i) = 0,\,\,\, \textrm{var}(\varepsilon_i) = \sigma^2, & i=1,2,\cdots n\\
    {\color{red}\textrm{cov}(\varepsilon_i,\varepsilon_j) = 0}, &  {\color{red}i\neq j, \,\, i,j=1,2,\cdots n}
    \end{array}\right.
    \end{eqnarray*}
    
    \end{enumerate}

\item 自相关是指实际中不同误差项的协方差并不等于零，即
\begin{eqnarray*}
{\color{red}\textrm{cov}(\varepsilon_i,\varepsilon_j) \neq 0, \,\,\exists i\neq j}
\end{eqnarray*}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.2. 自相关的原因} 

\begin{itemize}

\item 第一种情况：
\begin{itemize}
\item {\color{red}时间序列的数据中经常出现自相关现象。}
\item 经济变量有其周期性，变量之间的影响有滞后性。
\end{itemize}

\item 第二种情况：
\begin{itemize}
\item {\color{red}模型中缺少关键变量的话，关键变量的信息就会出现在误差项里。}
\item 如果关键变量在时间上有自相关性，就导致了误差项的自相关性。
\end{itemize}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.3. 自相关带来的问题} 

\begin{itemize}

\item {\color{red}普通最小二乘法的参数估计的方差较大，参数估计不准。}
\item 均方误差低估了误差项的方差。
\item $t$ 检验和 $F$ 检验不能如实检验参数和模型有效性。
\item 不正确的模型会影响回归方程的应用效果。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.4. 自相关性的检验方法1：图示检验法} 

\begin{enumerate}

\item 建立线性回归模型 $y=\beta_0 + \beta_1x+\varepsilon$.

\item 用普通最小二乘法求出残差 $e_1,e_2,\cdots, e_n$.

\item 画出 $n-1$ 个点 $\{(e_i,e_{i+1})\}_{i=1}^{n-1}$ 的散点图。

\item 如果大部分点落在第一、三象限，说明误差项存在正相关。%如果是第二、四象限的话则是负相关。

\item 记 $\rho$ 是残差项的一阶自相关系数，建立自回归模型
\[ \boxed{\color{red}\varepsilon_t = \rho \varepsilon_{t-1} + u_t,\,\, t=2,3,\cdots,n } \]

\item 这里 $u_t$ 是相互独立的扰动项。符合线性模型的基本假设。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.5. 自相关性的图示检验法(例子)}  

\begin{itemize}

%\item 每年的收入和支出两个数据称为1组数据，一共有23组数据，有23个残差。
%\item 相邻两年的残差 $(e_i,e_{i+1})$ 分别为横坐标与纵坐标，这样共有22个点。可见相邻两年的残差呈现正相关。
\item 左图：横坐标是收入，纵坐标是残差。
\item 右图：横坐标是这年的残差，纵坐标是下一年的残差。
\item {\color{red}结论：残差序列呈现一阶正相关。}

\begin{center}
%\centering
\includegraphics[height=0.45\textheight,width=0.9\textwidth]{income-spend-cct-zxgcct.png}
\end{center}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.6. 自相关性的检验方法2：自相关系数法}  

\begin{enumerate}

\item 自相关系数 $\rho$ 的估计值为残差向量 $(e_1,\cdots,e_{n-1})$ 和残差向量\\ $(e_2,\cdots,e_{n})$ 的 Pearson 相关系数：
\[ {\color{red} \hat{\rho} = \frac{e_1e_2+e_2e_3+\cdots+e_{n-1}e_n}{\sqrt{e_1^2+e_2^2+\cdots+e_{n-1}^2}\sqrt{e_2^2+e_3^2+\cdots+e_n^2}}  }\]

\item 计算自相关系数 $\hat{\rho}$, 结合样本量，进行 $\rho=0$ 的假设检验。

\item 实际问题中常用 DW 检验代替对 $\hat\rho$ 的检验。$\hat{\rho}$ 与 DW 统计量的关系是
\begin{eqnarray*}
\hat{\rho} \approx 1-\frac{DW}{2}
\end{eqnarray*}

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.7. 自相关性的检验方法3：DW检验}  

\begin{enumerate}
\item DW (Durbin-Watson) 统计量的定义是
\begin{eqnarray*}
{\color{red} DW = \frac{(e_2-e_1)^2+\cdots+(e_n-e_{n-1})^2}{e_2^2+e_3^2+\cdots+e_n^2} }
\end{eqnarray*}

\item DW统计量用于检验误差项有一阶自回归的自相关问题。

\item 根据样本量 $n$ 和自变量个数 $p$, 查DW分布表，得到临界值 $d_L$ 和 $d_U$.

\item 判别方法：
    \begin{itemize}
    \item {\color{red} 当 $0\le DW\le d_L$ 时认为误差项有正自相关。}
    \item 当 $4-d_L\le DW\le 4$ 时认为误差项有负自相关。
    \item 当 $DW\approx 2$ 时认为误差项没有自相关。
    \item 其余两种情形无法判断自相关性。
    \end{itemize}

\end{enumerate}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.8. 自相关性的检验和处理方法小结}  

\begin{itemize}%\setlength{\itemsep}{0.15cm}
\item 检验方法：
    \begin{itemize}%\setlength{\itemsep}{0.15cm}
    \item 图示检验法
    \item 自相关系数法
    \item DW检验
    \end{itemize}

\item 处理方法：
    \begin{itemize}%\setlength{\itemsep}{0.15cm}
    \item 迭代法
    \item 差分法
%    \item BOX-COX变换
    \end{itemize}
    
\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.9. 自相关的处理：迭代法(1-3) } 

\begin{enumerate}
\item[1.] 误差项存在一阶自相关的线性回归模型
\begin{eqnarray*}
y_t = \beta_0 + \beta_1 x_t + \varepsilon_t,\,\,\,\,\, 
\boxed{\color{red}\varepsilon_t = \rho \varepsilon_{t-1} + u_t}
\end{eqnarray*}

%\item[2.] 模型假设：
%\begin{eqnarray*}
%\mathbb{E}(u_t)=0, \textrm{var}(u_t)=\sigma^2, && t=1,2,\cdots, n \\
%{\color{red}\textrm{cov}(u_t,u_s)=0}, && {\color{red}t\neq s,\,\, t,s=1,2,\cdots, n}
%\end{eqnarray*}

\item[2.] 将下述两式相减，可以消去模型中的 $\varepsilon_t$.
\begin{eqnarray*}
y_t &=& \beta_0 + \beta_1 x_t + \varepsilon_t \\
\rho\cdot y_{t-1}  &=& \rho \cdot \left( \beta_0 + \beta_1 x_{t-1} + \varepsilon_{t-1} \right) \\
\longrightarrow
{\color{red}(y_t -\rho y_{t-1})} &=& (1-\rho)\beta_0 + \beta_1 {\color{red}(x_t-\rho x_{t-1})} + u_t
\end{eqnarray*}

\item[3.] 因此作如下数据变换和参数变换
\begin{eqnarray*}
\left\{\begin{array}{l}
y_t^*= y_t - \rho y_{t-1} \\
x_t^* = x_t - \rho x_{t-1} \\
\end{array}\right.
\hspace{0.5cm}
\left\{\begin{array}{l}
\beta_0^* =(1-\rho)\beta_0 \\
\beta_1^* =\beta_1 \\
\end{array}\right.
\end{eqnarray*}

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.10. 自相关的处理：迭代法(4-7)}  

\begin{enumerate}

\item[4.] 问题转化为下述线性回归模型，{\color{red}按假设误差项 $u_t$ 没有自相关性}：
\begin{eqnarray*}
y_t^* = \beta_0^* + \beta_1^* x_t^* + {\color{red}u_t}
\end{eqnarray*}

\item[5.] 用普通最小二乘法求出上述线性回归模型的参数 $(\beta_0^*,\beta_1^*)$.

\item[6.] 计算残差，一般来讲，自相关性已经消除。%如果残差仍然存在一阶自相关，返回第一步。

\item[7.] 代回得到原模型的参数估计 $({\beta}_0,{\beta}_1)$, 以及模型：
\begin{eqnarray*}
y_t &=& {\beta}_0 + {\beta}_1 x_t + \varepsilon_t \\
\varepsilon_t &=& {\rho} \varepsilon_{t-1} + u_t 
\end{eqnarray*}

\vspace{0.2cm}

\item[*] 注：如果使用 $\rho=1$,  那就是差分法。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.11. 例子4-5：支出对收入的回归模型(变量与问题)}  

\begin{itemize}

\item 考虑的因素如下：
\begin{center}
\begin{tabular}{|l|l|}\hline
变量 & 含义（单位） \\ \hline
$y$ & 人均支出（元） \\ \hline
$x$ & 人均收入（元） \\ \hline
\end{tabular}
\end{center}

\item 线性回归模型：
\begin{eqnarray*}
y = \beta_0 + \beta_1x + \varepsilon
\end{eqnarray*}

\item 待解决问题：
        \begin{enumerate}
        \item 建立线性回归模型。
        \item 诊断残差是否存在自相关性。
        \item 处理自相关性，得到更准确的回归模型。
        \end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.12. 例子4-5：支出对收入的回归模型(数据)} 

{\footnotesize 
\begin{center}
\begin{tabular}{|c|c|c||c|c|c|}\hline
年份 & 人均支出$y$ & 人均收入$x$ & 年份 & 人均支出$y$ & 人均收入$x$  \\ \hline
1990 & 1278 & 1510 & 2002 & 6029 & 7702   \\ \hline
1991 & 1453 & 1700 & 2003 & 6510 & 8472    \\ \hline
1992	 & 1671 & 2026 & 2004 & 7182 & 9421    \\ \hline
1993	 & 2110 & 2577 & 2005 & 7942 & 10493    \\ \hline
1994 & 2851 & 3496 & 2006 & 8696 & 11759    \\ \hline
1995 & 3537 & 4282 & 2007 & 9997 & 13785    \\ \hline
1996 & 3919 & 4838 & 2008 & 11242 & 15780    \\ \hline
1997 & 4185 & 5160 & 2009 & 12264 & 17174    \\ \hline
1998 & 4331 & 5425 & 2010 & 13471 & 19109    \\ \hline
1999 & 4615 & 5854 & 2011 & 15160 & 21809    \\ \hline
2000 & 4998 & 6279 & 2012 & 16674 & 24564    \\ \hline
2001 & 5309 & 6859 & & &  \\ \hline
\end{tabular}
\end{center}
}

%\item 
%数据文件：ex5-2.csv. \\
%程序文件：test21.py.

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.13. 例子4-5解答：回归模型、残差图} 

\begin{itemize}

\item 建立线性回归模型：$\boxed{\hat{y} = 608.87 + 0.6732 x}$

\item 左图：数据散点图和回归直线，右图：横坐标为自变量的残差图。\\
看到残差与自变量之间有一定规律。

\begin{center}
\includegraphics[height=0.5\textheight,width=0.9\textwidth]{income-spend-hgt-cct.png}
\end{center}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.14. 例子4-5解答：残差的自相关图}  

\begin{itemize}

%\item 每年的收入和支出两个数据称为1组数据，一共有23组数据，有23个残差。
%\item 相邻两年的残差 $(e_i,e_{i+1})$ 分别为横坐标与纵坐标，这样共有22个点。可见相邻两年的残差呈现正相关。
\item 左图：横坐标是收入，纵坐标是残差。
\item 右图：横坐标是这年的残差，纵坐标是下一年的残差。看到相邻两年的残差呈现正相关。
\begin{center}
%\centering
\includegraphics[height=0.5\textheight,width=0.9\textwidth]{income-spend-cct-zxgcct.png}
\end{center}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.15. 例子4-5解答：用 DW检验判断自相关} 

\begin{itemize}

\item 计算残差的自相关系数与DW统计量：
\begin{eqnarray*}
{\color{red}\hat{\rho}=0.8286,\,\,\, DW=0.3247. }
\end{eqnarray*}

\item 查DW表知，当 $n=23$, $p=2$, $\alpha=0.05$ 时，$d_L=1.26$, $d_U=1.44$. 

\item 于是 $0<DW<d_L$, 结论是残差项有正自相关。

\end{itemize}

\begin{center}
%\centering
\includegraphics[height=0.4\textheight,width=0.7\textwidth]{income-spend-dw.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.16. 例子4-5解答：用迭代法处理自相关 (1-3)}  

\begin{enumerate}

\item[1.] 作变量代换，得到新数据 $(x_t^*,y_t^*),1\le t\le 22$:
\begin{eqnarray*}
\left\{\begin{array}{l}
y_t^* = y_t - \hat{\rho} y_{t-1} \\
x_t^* = x_t - \hat{\rho} x_{t-1} \\
\end{array}\right.
\end{eqnarray*}

\item[2.] 用普通最小二乘法，得到回归方程
\begin{eqnarray*}
{\color{red}\boxed{y_t^* = 202.17 + 0.6332 x_t^* + u_t}}
\end{eqnarray*}

\item[3.] 计算新数据的回归模型的残差，检验自相关性。得到 $\rho=-0.018$, $DW=1.81$, 认为{\color{red}残差项 $u_t$ 没有自相关性}。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.4.17. 例子4-5解答：用迭代法处理自相关(4-5)}  

\begin{enumerate}

\item[4.] 残差项 $u_t$ 的图形，没显示出自相关的特征。

\includegraphics[height=0.25\textheight,width=0.9\textwidth]{income-spend-bldh-cct.png}

\item[5.] 写回原来的自变量 $x$ 和因变量 $y$, 得到回归方程为
\begin{eqnarray*}
 y_t - \hat{\rho} y_{t-1} = 202.17 + 0.6332 (x_t - \hat{\rho} x_{t-1}) + u_t
\end{eqnarray*}
代入 $\hat{\rho}=0.8286$, 化简整理得到
\begin{eqnarray*}
{\color{red}\boxed{ y_t  = 202.17 + 0.8286 y_{t-1} + 0.6332 x_t - 0.5247 x_{t-1} + u_t}}
\end{eqnarray*}
其中 $u_t$ 是没有自相关性的误差项。

%\item[5.] 作参数的变量代换，得到原方程的参数
%\begin{eqnarray*}
%%\left\{\begin{array}{l}
%\beta_0 =\beta_0^*/(1-\rho), \hspace{0.3cm}
%\beta_1 =\beta_1^* 
%%\end{array}\right.
%\end{eqnarray*}
%
%\item[6.] 得到经过迭代法处理的回归模型
%\begin{eqnarray*}
%{\color{red}\boxed{
%\left\{\begin{array}{l}
%y_t = 1179.68 + 0.6332 x_t + e_t \\
%e_t = 0.8286 e_{t-1} + u_t
%\end{array}\right.
%}}
%\end{eqnarray*}

\end{enumerate}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.4.18. 例子4-5解答：Python程序(1)}  %若使用verbatim环境
%\begin{frame}{4.4.18. 例子4-5解答：} 

\begin{itemize}
\item 载入模块，读入数据，建立普通最小二乘回归模型：

{\small {\color{blue}
\begin{verbatim}
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import statsmodels.formula.api as smfa
import statsmodels.api as sma

mydata=pd.read_csv('income-spend.csv')  
my_result=smfa.ols(formula='y~x',data=mydata).fit() 

e = my_result.resid 
beta = my_result.params
\end{verbatim}
}}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.4.19. 例子4-5解答：Python程序(2)}  %若使用verbatim环境
%\begin{frame}{4.4.19. 例子4-5解答：} 

\begin{itemize}
\item 画散点图和回归直线：

{\small {\color{blue}
\begin{verbatim}
x=mydata['x']; y=mydata['y']
yhat=beta[0]+beta[1]*x; e_check=y-yhat

y0=np.zeros(len(x)); 
x1=np.linspace(x.min()*0.5,x.max()*1.05,100)
y1=beta[0]+beta[1]*x1; y2=np.zeros(len(x1))
e1=np.array(e[:-1]); e2=np.array(e[1:])

plt.plot(x,y,'b.'); plt.plot(x1,y1,'--')
plt.xlabel('x=income'); plt.ylabel('y=spend')

\end{verbatim}
}}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.4.20. 例子4-5解答：Python程序(3)}  %若使用verbatim环境
%\begin{frame}{4.4.20. 例子4-5解答：} 

\begin{itemize}
\item 画残差图和残差的自相关图：

{\small {\color{blue}
\begin{verbatim}
plt.subplot(121); plt.plot(x,e,'b.'); plt.plot(x1,y2,'--')
plt.xlabel('x = income'); plt.ylabel('e = residual')

plt.subplots_adjust(wspace=0.6)
plt.plot(e1,e2,'b.'); plt.axhline(y=0); plt.axvline(x=0)
plt.xlabel('$e_t$'); plt.ylabel('$e_{t+1}$')
\end{verbatim}
}}

\item 计算残差 $e_t$ 的自相关系数与DW统计量：

{\small {\color{blue}
\begin{verbatim}
rho=np.dot(e1,e2)/np.sqrt(np.dot(e1,e1))/np.sqrt(np.dot(e2,e2))
dw=np.sum((e2-e1)**2)/np.sum(e2**2)
\end{verbatim}
}}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.4.21. 例子4-5解答：Python程序(4)}  %若使用verbatim环境
%\begin{frame}{4.4.21. 例子4-5解答：} 

\begin{itemize}
\item 数据进行变量代换，再最小二乘回归：

{\small {\color{blue}
\begin{verbatim}
xp=np.array(x[1:])-rho*np.array(x[:-1])
yp=np.array(y[1:])-rho*np.array(y[:-1])
Xp=sma.add_constant(xp); my_result_2=sma.OLS(yp,Xp).fit()
my_beta_2=my_result_2.params
beta0=my_beta_2[0]/(1-rho); beta1=my_beta_2[1]
\end{verbatim}
}}

\item 检查 $u_t$ 的自相关性，计算自相关系数与DW统计量：
{\small {\color{blue}
\begin{verbatim}
yhatp=my_beta_2[0]+my_beta_2[1]*xp
u=yp-yhatp; u1=np.array(u[:-1]); u2=np.array(u[1:])
rhop=np.dot(u1,u2)/np.sqrt(np.sum(u1**2))/np.sqrt(np.sum(u2**2))
dwp=np.sum((u2-u1)**2)/np.sum(u2**2)
\end{verbatim}
}}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.5.1. BOX-COX变换}

\begin{itemize}

\item {\color{red}问题：什么是BOX-COX变换？ }

\item 解答：设变量 $y$ 取值大于零，设 $\lambda$ 是待定参数，BOX-COX变换将 $y$ 变成
$$
y^{(\lambda)} = \left\{ \begin{array}{ll}
\frac{y^\lambda-1}{\lambda}, & \lambda\neq 0, \\
\ln y, & \lambda=0. 
\end{array}\right.
$$

\item  一些常用的变换：
\begin{eqnarray*}
y^{(0)} &=& \ln y, \\ 
y^{(1/2)} &=& \sqrt{y}, \\ 
y^{(-1)} &=& 1-\frac{1}{y}. 
\end{eqnarray*}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.5.2. }

\begin{itemize}

\item {\color{red}问题：寻找BOX-COX变换的参数$\lambda$? 的目标是什么？ } 

\item 解答：寻找合适的 $\lambda$, 使得 $y^{(\lambda)}$ 符合线性回归模型的各项假设，
\begin{eqnarray*}
y^{(\lambda)} = \begin{pmatrix} y_1^{(\lambda)} \\ y_2^{(\lambda)} \\ \vdots \\ y_n^{(\lambda)}  \end{pmatrix}
\sim N_n(X\beta, \sigma^2 I_n). 
\end{eqnarray*}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.5.3. }

\begin{itemize}

\item {\color{red}问题：如何寻找BOX-COX变换的参数$\lambda$?} 

\item 解答：
\begin{enumerate}
\item  求出 $\lambda$ 的最大似然估计。
\item  使用MASS包里的  boxcox() 函数。
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.5.4. }

\begin{itemize}

\item {\color{red}问题：举例说明 BOX-COX变换可以消除异方差。} 

\item 解答：考虑例子3-2（表格3-6），北京市各经济开发区经济发展与招商投资的关系。

\begin{table}\centering 
\begin{tabular}{|c|c|c|c|}\hline 
序号 & 招商数目 x1&	招商企业注册资本x2&  	销售收入y \\ \hline 
1& 25&	3547.79&	553.96 \\ \hline 
2& 20&	896.34&	208.55 \\ \hline 
3& 6&	750.32&	3.1 \\ \hline 
$\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ \\ \hline 
%4& 1001&	2087.05&	2815.4 \\ \hline 
%5& 525&	1639.31&	1052.12 \\ \hline 
%6& 825&	3357.7&	3427 \\ \hline 
%7& 120&	808.47&	442.82 \\ \hline 
%8& 28&	520.27&	70.12 \\ \hline 
%9& 7&	671.13&	122.24 \\ \hline 
%10& 532&	2863.32&	1400 \\ \hline 
%11& 75&	1160&	464 \\ \hline 
%12& 40&	862.75&	7.5 \\ \hline 
%13& 187&	672.99&	224.18 \\ \hline 
14& 122&	901.76&	538.94 \\ \hline 
15& 74&	3546.18&	2442.79 \\ \hline 
\end{tabular}
\end{table}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.5.5.  } %若使用verbatim环境
%\begin{frame}{4.5.5. }

\begin{lstlisting}[language=R]
library(MASS)
mydata=read.csv('table-3-6.csv',head=T)
mydata
bc32=boxcox(y~x1+x2,data=mydata,lambda=seq(-2,2,0.01))
lambda=bc32$x[which.max(bc32$y)]
lambda
y_bc=(mydata$y^lambda-1)/lambda
lm32_bc=lm(y_bc~x1+x2,data=mydata)
summary(lm32_bc)
abse=abs(resid(lm32_bc))
cor.test(mydata$x1,abse,method='spearman')
cor.test(mydata$x2,abse,method='spearman')
\end{lstlisting}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.5.6. }

\begin{itemize}

\item  使得似然函数取之最大的 $\lambda=0.47$. 
\item  数据变换后的回归方程为 $\hat{y}^{(0.47)} = 6.80 + 0.05x_1+0.014x_2. $
\item  代入数据变换公式 $$\hat{y}^{(0.47)} = \frac{\hat{y}^{0.47}-1}{0.47},$$
得到还原原始变量的回归方程为 $\hat{y} = (4.196 + 0.024x_1+0.007x_2)^{\frac{1}{0.47}}. $
\item  残差绝对值与两个自变量的等级相关系数检验的p值都大于0.05, 异方差情况不显著。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.5.7. }

\begin{itemize}

\item {\color{red}问题：举例说明 BOX-COX变换可以消除自相关。} 

\item 考虑例子2-2（表格2-2），我国城镇居民人均支出和人均收入之间的关系。

\begin{table}\centering 
\begin{tabular}{|c|c|c|}\hline 
年份 &  人均支出y&	人均收入x \\ \hline 
1990&	1278.89&	1510.16 \\ \hline 
1991&	1453.8&	1700.6 \\ \hline 
1992&	1671.7&	2026.6 \\ \hline 
$\vdots$ & $\vdots$ & $\vdots$ \\ \hline 
%1993	2110.8	2577.4
%1994	2851.3	3496.2
%1995	3537.57	4282.95
%1996	3919.5	4838.9
%1997	4185.6	5160.3
%1998	4331.6	5425.1
%1999	4615.9	5854
%2000	4998	6279.98
%2001	5309.01	6859.6
%2002	6029.92	7702.8
%2003	6510.94	8472.2
%2004	7182.1	9421.6
%2005	7942.88	10493
%2006	8696.55	11759.5
%2007	9997.47	13785.8
%2008	11242.85	15780.76
%2009	12264.55	17174.65
%2010	13471.45	19109.4
2011&	15160.89&	21809.8 \\ \hline 
2012&	16674.32&	24564.7 \\ \hline 
\end{tabular}
\end{table}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.5.8. } %若使用verbatim环境
%\begin{frame}{4.5.8. }

\begin{lstlisting}[language=R]
library(MASS)
mydata=read.csv('table-2-2.csv',head=T)
mydata
bc22=boxcox(y~x,data=mydata,lambda=seq(-2,2,0.01))
lambda=bc22$x[which.max(bc22$y)]
lambda
y_bc=(mydata$y^lambda-1)/lambda
lm22_bc=lm(y_bc~x,data=mydata)
summary(lm22_bc)
\end{lstlisting}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.5.9. }


\begin{center}
\includegraphics[height=0.8\textheight,width=0.9\textwidth]{example-4-5-2.png}
\end{center}


\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.5.10. }

\begin{itemize}

\item  参数 $\lambda$ 的最大似然估计 $\lambda=1.15$. 
\item  $\hat{y}^{(1.15)}$ 对 $x$ 的回归方程是 $\hat{y}^{(1.15)} = -716.6 + 2.579x$. 
\item  BOX-COX变量代换公式为 $$\hat{y}^{(1.15)} = \frac{y^{1.15}-1}{1.15}. $$
\item  还原原始变量的回归方程是 $\hat{y} = (-823.090 + 2.966x)^{\frac{1}{1.15}}$. 
\item  残差DW检验p值大于0.05，序列自相关情况不显著。

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.6.1. 一元线性回归的基本符号} 

{\small
\begin{center}
\begin{tabular}{|p{1.3cm}|p{4cm}|p{5cm}|}\hline
符号 & 计算公式 & 名称 \\ \hline
$x_i$, $y_i$ & 测量   & 变量 $x,y$ 的第 $i$ 个观测值 \\  \hline
${\bf x}$ & ${\bf x}=(x_1,\cdots,x_n)^T$  & $x$的$n$个观测值的列向量 \\  \hline
${\bf y}$ & ${\bf y}=(y_1,\cdots,y_n)^T$  & $y$的$n$个观测值的列向量 \\  \hline
$X$ & $X=[{\bf 1}^T, {\bf x}^T]$ & 设计矩阵 \\ \hline
$\hat\beta$ & $\hat\beta=(X^TX)^{-1}X^T{\bf y}$ & 参数的最小二乘估计 \\ \hline
$\hat{\bf y}$ & $\hat{\bf y}=X\hat\beta=H{\bf y}$  & $y$的$n$个回归值的列向量 \\  \hline
$\hat{y}_i$ & $\hat{y}_i=\hat{\beta}_0+\hat{\beta}_1x_{i}$ & 第 $i$ 个回归值 \\ \hline
$e_i$ & $e_i= y_i - \hat{y}_i$  & 第 $i$ 个残差 \\  \hline
$\hat\sigma^2$ & $\hat\sigma^2=\frac{1}{n-2}\sum_{i=1}^{n} e_i^2 $ & 残差的方差 \\ \hline
$H$ & $H= X(X^TX)^{-1}X^T$ & 帽子矩阵 \\  \hline
$h_{ii}$ & $h_{ii}$= $H$的第$(i,i)$元素 & 第 $i$ 个观测值的杠杆值 \\ \hline
\end{tabular}
\end{center}
}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.6.2. 删除学生化残差的计算}  

\begin{itemize}

\item 第 $i$ 组观测值 $(x_i,y_i)$ 的{\color{red}残差 $e_i$} 的计算如下。其中回归系数 $\hat{\beta}$ 是用所有数据通过最小二乘法计算得到。
\[ e_i = y_i -\hat{y}_i =y_i - (\hat{\beta}_0+\hat{\beta}_1x_i) \]

\item 第 $i$ 组观测值 $(x_i,y_i)$ 的{\color{red}删除残差 $e_{(i)}$} 的计算如下。用这组观测值之外的其余 $n-1$ 组观测值作回归方程，然后代入 $x_i$ 来计算回归值 $\hat{y}_{(i)}$, 然后计算观测值与回归值的差。
\[ e_{(i)}=y_i-\hat{y}_{(i)} \]

\item {\color{red}删除学生化残差}可以通过学生化残差来计算：
\[ sre_{(i)} = sre_i\cdot\sqrt{\frac{n-p-2}{n-p-1-sre_i^2}} \]

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.6.3. 异常值与强影响点的概念}  

\begin{itemize}

%\item 复习基本符号：
%    \begin{itemize}
%    \item 残差 $e_i=y_i-\hat{y}_i$, 拟合值 $\hat{y}_i = \hat{\beta}_0+ \hat{\beta}_1x_i$. %, $1\le i\le n$.
%    \item 帽子矩阵 $H=X(X^TX)^{-1}X^T$, 其对角线元素 $h_{ii}$ 称为杠杆值。 
%    \end{itemize}
    
\item 异常值是数据中的极端的观测值，可能来自其它数据或其他模型。
%其残差的绝对值比其他残差大很多，是因变量数据空间的离群点。
    \begin{itemize}
    \item 响应变量中的异常值：
    若标准化残差 $sre_i=\frac{e_i}{\hat\sigma\sqrt{1-h_{ii}}}$ 的绝对值大于2，则对应的观测点称为{\color{red}异常点}。
    \item 预测变量中的异常值：
    若第 $i$ 个观测点的杠杆值 $h_{ii}$ 大于 $\frac{2(p+1)}{n}$, 则称对应的观测点为{\color{red}高杠杆点}。
    \end{itemize}

\item 如果删除一个观测点会导致拟合模型的实质性变化，即系数估计值、拟合值和检验值等发生较大变化，则称这个点为{\color{red}强影响点}。

%\item 处理：删除或重新观测，加权回归，增加自变量，增加数据，改变模型，等。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.6.4. 强影响点的检验}  

\begin{itemize}

\item {\color{red}库克距离的定义：所有数据的模型的参数估计与删除第 $i$ 个数据的模型的参数估计之间的距离的平方，称为第 $i$ 个点的库克距离。}

\item 库克距离的计算公式：
\begin{eqnarray*}
D_i = \frac{e_i^2}{(p+1)\hat\sigma^2}\cdot\frac{h_{ii}}{(1-h_{ii})^2}
=\frac{sre_i^2}{(p+1)}\cdot\frac{h_{ii}}{(1-h_{ii})}.
\end{eqnarray*}

\item 强影响点是指保留该点与删除该点两种情况下建立的回归方程中的回归系数会产生很大差异的点。检验方法：
    \begin{itemize}
    \item 检验1：若杠杆值 $h_{ii}$ 较大，则第 $i$ 个点是强影响点。
    \item 检验2：若库克距离 $D_i$ 较大，则第 $i$ 个点是强影响点。
    \end{itemize}

%\item 解释库克距离：
%\begin{itemize}
%\setlength{\itemsep}{0.1cm}
%\item 若残差较大，则库克距离也大。
%\item 若杠杆值 $h_{ii}$ 较大，则库克距离也大。
%\end{itemize}

\end{itemize}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.6.5. 例子*：数据与待完成的问题}  

\begin{itemize}

\item 数据：一共有12个 $(x,y)$的数据如下表。

\begin{center}
\begin{tabular}{|c|c|c||c|c|c|}\hline
$i$ & $y$ & $x$ & $i$ & $y$ & $x$ \\ \hline
1& 8.11& 0& 7& 9.60& 19   \\ \hline
2& 11.00& 5& 8& 10.30& 20  \\ \hline
3& 8.20& 15&9& 11.30& 21 \\ \hline 
4& 8.30& 16&10& 11.40& 22 \\ \hline 
5& 9.40& 17& 11& 12.20& 23 \\ \hline 
6& 9.30& 18& 12& 12.90& 24 \\ \hline 
\end{tabular}
\end{center}

\item 任务：识别非正常观测点。
\item 数据来源：S. Chatterjee, A.S. Hadi, Regression Analysis by Example, 5th Edition, Wiley, 2012. 
郑忠国，许静(译), 机械工业出版社，2018年。

\end{itemize}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.6.6. 例子解答(1)：四个模型的参数估计} 

\begin{itemize}
\item 按照是否删除第1、2个点，建立四个回归模型，得到的参数估计：

\begin{center}
\begin{tabular}{|p{3cm}|p{2cm}|p{2cm}|}\hline
数据 & $\beta_0$ & $\beta_1$  \\ \hline
全部数据 & 8.1097 & 0.1235  \\ \hline
删除第1个点 & 8.1093 & 0.1235  \\ \hline
删除第2个点 & 6.7054 & 0.191   \\ \hline
删除第1,2个点 & 0.0909 & 0.523   \\ \hline
\end{tabular}
\end{center}

\item 删除第1个点，基本上没影响；删除第2个点，有较大影响；\\ 同时删除这两个点，有很大影响。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.6.7. 例子解答(2)：四个模型的回归线}  

\begin{itemize}

\item 按照是否删除第1、2个点，建立四个回归模型，得到的回归线。

\vspace{-0.15cm}

\begin{center}
\includegraphics[height=0.65\textheight,width=0.9\textwidth]{hadi-p126-four-models-sdt.png}
\end{center}

\end{itemize}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.6.8. 例子解答(3)：计算各判别量} 

\begin{itemize}

\item 计算残差、杠杆值和库克距离：
\begin{center}
%\begin{tabular}{|p{2cm}|p{2.3cm}|p{2.7cm}|p{2cm}|}\hline
\begin{tabular}{|c|c|c|c|}\hline
观测点 & 学生化残差 & 杠杆值 & 库克距离   \\ \hline
检验依据 & $|sre_i|> 2$ & $h_{ii}> (2p+2)/n$ & 显著较大    \\ \hline
第1个点 & $0.000$ & $0.565$ & $0.000$  \\ \hline
第2个点 & $1.969$ & $0.319$ & $0.910$ \\ \hline
\end{tabular}
\end{center}

\item 可得如下结论：
{\color{red}
\begin{center}
%\begin{tabular}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}\hline
\begin{tabular}{|c|c|c|c|}\hline 
观测点 & 异常点 & 高杠杆点 & 强影响点   \\ \hline
第1个点 & 不是 & 是 & 不是  \\ \hline
第2个点 & 不是 & 不是 & 是 \\ \hline
\end{tabular}
\end{center}
}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{4.6.9. 例子解答(4)：各判别量的图形} 

\begin{itemize}

\item 分别为回归线、学生化残差图、杠杆值、库克距离的图形。

\begin{center}
\includegraphics[height=0.65\textheight,width=0.7\textwidth]{hadi-p126-hgx-sre-leverage-cook.png}
\end{center}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.6.10. 例子解答：Python程序(1)} %若使用verbatim环境
%\begin{frame}{4.6.10. 例子解答：} 

\begin{itemize}
\item 载入模块，计算所有数据的回归模型：

{\small {\color{blue}
\begin{verbatim}
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import statsmodels.formula.api as smfa
import statsmodels.api as sma
#import scipy.stats as stats

mydata = pd.read_csv('hadi-p126.csv') 
myrst=smfa.ols(formula='y~x', data=mydata).fit()
beta=myrst.params
\end{verbatim}
}}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.6.11. 例子解答：Python程序(2)} %若使用verbatim环境
%\begin{frame}{4.6.11. 例子解答：} 

\begin{itemize}
\item 分别删除第1、2个点，计算回归模型：

{\small {\color{blue}
\begin{verbatim}
mydf_d0=mydata.drop(index=0)
myrst_d0=smfa.ols(formula='y~x',data=mydf_d0).fit()
beta_d0=myrst_d0.params
mydf_d1=mydata.drop(index=1)
myrst_d1=smfa.ols(formula='y~x',data=mydf_d1).fit()
beta_d1=myrst_d1.params
mydf_d01=mydata.drop(index=[0,1])
myrst_d01=smfa.ols(formula='y~x',data=mydf_d01).fit()
beta_d01=myrst_d01.params

beta_4_models=np.vstack([beta,beta_d0,beta_d1,beta_d01])
\end{verbatim}
}}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.6.12. 例子解答：Python程序(3)} %若使用verbatim环境
%\begin{frame}{4.6.12. 例子解答：} 

\begin{itemize}
\item 计算帽子矩阵和杠杆值，学生化残差和库克距离：

{\small {\color{blue}
\begin{verbatim}
x=mydata['x']; y=mydata['y']
x1=np.linspace(x.min()*0.5,x.max()*1.05,100)
y1=beta[0]+beta[1]*x1; y5=np.zeros(len(x1))

X=sma.add_constant(x); B=np.dot(X.T,X); Bi=np.linalg.inv(B)
H=np.dot(np.dot(X,Bi),X.T); h=np.diag(H)

e=myrst.resid; p=1; n=myrst.nobs
sigmahat=np.sqrt(sum(e**2)/(n-p-1))
sre=e/sigmahat/np.sqrt(1-h)
cook_d=e**2*h/(p+1)/(sigmahat**2)/((1-h)**2)
\end{verbatim}
}}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{4.6.13. 例子解答：Python程序(4)} %若使用verbatim环境
%\begin{frame}{4.6.13. 例子解答：} 

\begin{itemize}
\item 画出散点图、学生化残差、杠杆值图、库克距离图：

{\small {\color{blue}
\begin{verbatim}
plt.subplot(221); plt.plot(x,y,'b.',label='')
plt.plot(x1,y1,'--',label='full data')
plt.xlabel('x'); plt.ylabel('y'); plt.legend()
plt.subplot(222); plt.subplots_adjust(wspace=0.4)
plt.plot(x,sre,'b.'); plt.plot(x1,y5,'--')
plt.xlabel('x'); plt.ylabel('studentized residuals')

plt.subplot(223); plt.subplots_adjust(hspace=0.4)
plt.stem(x,h); plt.xlabel('x'); plt.ylabel('leverage')
plt.subplot(224); plt.stem(x,cook_d)
plt.xlabel('x'); plt.ylabel('cook distance')
\end{verbatim}
}}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}



